<template>
	<view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				navIndex: 1,
				targetObj: '',
				value: '',
				cScanInputH: '',
				barcode: {},
				flashLampObj: null,
				isFlash: false,
				timer: null,
			}
		},
		onLoad(options) {
			this.navIndex = options.navIndex || 1
		},
		onReady() {
			this.scan();
			setTimeout(() => {
				this.flashLamp();
				this.drawTips()
				this.back()
				if (this.navIndex != -1) {
					// this.navItem();
					this.navItem2();
					this.navItem3();
				}
			}, 500)

		},
		destroyed() {
			this.closeView()
			clearTimeout(this.timer)
		},

		methods: {
			// 扫一扫
			scan() {
				var pages = getCurrentPages();
				var page = pages[pages.length - 1];
				// #ifdef APP-PLUS  
				var currentWebview = page.$getAppWebview(); //页面栈最顶层就是当前webview  
				// #endif
				// 创建扫描码
				var barcode = plus.barcode.create('barcode', [plus.barcode.CODE128, plus.barcode.QR, plus.barcode.EAN13,
					plus.barcode.EAN8
				], {
					top: '0px',
					left: '0px',
					width: '100%',
					height: '100%',
					position: 'static',
					background: '#FFFFFF',
					frameColor: '#FFFFFF',
					scanbarColor: '#FFFFFF'
				});


				// 成功
				barcode.onmarked = (type, result) => {
					let scanType = ''
					let url = ''
					console.log(this.navIndex);
					if (this.navIndex == 0) {
						scanType = 'inStorage'
						url = `/pages/scanCode/qrcodeResult?pageType=${scanType}&scanResult=${result}`
					} else if (this.navIndex == 1) {
						scanType = 'outStorage'
						url = `/pages/scanCode/qrcodeResult?pageType=${scanType}&scanResult=${result}`
					} else if (this.navIndex == 2) {
						scanType = 'chargeOff'
						url = `/pages/orderManagement/writeOffCoupon?pageType=${scanType}&scanResult=${result}`
					} else if(this.navIndex == -1) {
						scanType = 'addGoods'
						url = `/pages/scanCode/qrcodeResult?pageType=${scanType}&scanResult=${result}`
					}

					uni.redirectTo({
						url
					})
				};

				// 失败
				barcode.onerror = (error) => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				};

				this.barcode = barcode;
				currentWebview.append(barcode);
				barcode.start();
			},

			// 手电筒
			flashLamp() {
				// var that = this;
				// var view;

				// function _Create(flag) {
				//     var flashLamp = plus.nativeObj.View.getViewById('flashLamp');
				//     if (flashLamp) {
				//         flashLamp.close()
				//     }
				//     view = new plus.nativeObj.View('flashLamp', {
				//         top: '60%',
				//         left: '40%',
				//         height: '60px',
				//         width: '80px'
				//     });
				//     var color = flag ? '#794fc4' : '#ffffff';
				//     view.drawRichText('<font color="' + color +
				//         '" style="font-size:24px;">\ue65b</font><br/><font color="' + color +
				//         '" style="font-size:10px;">轻触照亮</font>', {
				//             top: '0px',
				//             left: '0%',
				//             width: '100%',
				//             height: 'wrap_content',
				//             color: 'FF0000'
				//         }, {
				//             size: '24px',
				//             family: 'Times New Roman',
				//             fontSrc: '/static/font/iconfont.ttf',
				//             align: 'center'
				//         });
				//     view.show();
				//     view.addEventListener("click", onClick, false);
				// }

				// _Create();

				// function onClick() {
				//     that.isFlash = !that.isFlash;
				//     that.barcode.setFlash(that.isFlash);
				//     _Create(that.isFlash)
				// }
			},
			back() {
				var that = this;
				var view;

				function _Create(flag) {
					view = new plus.nativeObj.View('back', {
						top: '50px',
						left: '20px',
						height: '40px',
						width: '40px'
					});
					var color = flag ? '#794fc4' : '#ffffff';
					// 绘制图片
					view.drawBitmap('/static/images/icon-back-white-40.png', {
						top: '0px',
						left: '0px',
						width: '40px',
						height: '40px',
						align: 'center'
					}, {
						top: '0',
						left: '0',
						width: '100%',
						height: '100%',
						align: 'center'
					});
					view.show();
					view.addEventListener("click", onClick, false);
				}

				_Create();

				function onClick() {
					uni.navigateBack({
						delta: 1
					})
				}
			},
			// 入库
			navItem() {
				var that = this;
				var view;

				function _Create(flag) {
					view = new plus.nativeObj.View('navItem', {
						bottom: '10%',
						left: '15%',
						height: '60px',
						width: '80px'
					});
					var color = flag ? '#794fc4' : '#ffffff';
					let line = ''
					if (that.navIndex == 0) {
						line = '<font color="' + color +
							'" style="font-size:24px;">\u2014</font>'
					}
					view.drawRichText('<div color="' + color +
						'"style="font-size:16px;">入库</div><br/>' + line, {
							top: '1px',
							left: '0%',
							width: '100%',
							height: 'wrap_content',
							color: 'FFFFFF',
						}, {
							size: '24px',
							family: 'Times New Roman',
							fontSrc: '/static/font/iconfont.ttf',
							align: 'center'
						});
					view.show();
					view.addEventListener("click", onClick, false);
				}

				_Create();

				function onClick() {
					that.navIndex = 0

					//                plus.nativeObj.View.getViewById('flashLamp').close();
					// plus.nativeObj.View.getViewById('navItem').close();
					plus.nativeObj.View.getViewById('navItem2').close();
					plus.nativeObj.View.getViewById('navItem3').close();
					//                plus.barcode.getBarcodeById('barcode').close();

					// that.navItem();
					that.navItem2();
					that.navItem3();
				}
			},
			// 出库
			navItem2() {
				var that = this;
				var view;

				function _Create(flag) {
					view = new plus.nativeObj.View('navItem2', {
						bottom: '10%',
						left: '15%',
						height: '60px',
						width: '80px'
					});
					var color = flag ? '#794fc4' : '#ffffff';
					let line = ''
					if (that.navIndex == 1) {
						line = '<font color="' + color +
							'" style="font-size:24px;">\u2014</font>'
					}
					view.drawRichText('<div color="' + color +
						'"style="font-size:16px;">出库</div><br/>' + line, {
							top: '1px',
							left: '0%',
							width: '100%',
							height: 'wrap_content',
							color: 'FFFFFF',
						}, {
							size: '24px',
							family: 'Times New Roman',
							fontSrc: '/static/font/iconfont.ttf',
							align: 'center'
						});
					// view = new plus.nativeObj.View('navItem2', {
					// 	bottom: '10%',
					// 	left: '39%',
					// 	height: '60px',
					// 	width: '80px'
					// });
					// var color = flag ? '#794fc4' : '#ffffff';
					// let line = ''
					// if (that.navIndex == 1) {
					// 	line = '<font color="' + color +
					// 		'" style="font-size:24px;">\u2014</font>'
					// }
					// view.drawRichText('<div color="' + color +
					// 	'"style="font-size:16px;">出库</div><br/>' + line, {
					// 		top: '0px',
					// 		left: '0%',
					// 		width: '100%',
					// 		height: 'wrap_content',
					// 		color: 'FFFFFF',
					// 	}, {
					// 		size: '24px',
					// 		family: 'Times New Roman',
					// 		fontSrc: '/static/font/iconfont.ttf',
					// 		align: 'center'
					// 	});
					view.show();
					view.addEventListener("click", onClick, false);
				}

				_Create();

				function onClick() {
					that.navIndex = 1
					// plus.nativeObj.View.getViewById('flashLamp').close();
					// plus.nativeObj.View.getViewById('navItem').close();
					plus.nativeObj.View.getViewById('navItem2').close();
					plus.nativeObj.View.getViewById('navItem3').close();
					// plus.barcode.getBarcodeById('barcode').close();

					// that.navItem();
					that.navItem2();
					that.navItem3();
				}
			},
			// 核销
			navItem3() {
				var that = this;
				var view;

				function _Create(flag) {

					view = new plus.nativeObj.View('navItem3', {
						bottom: '10%',
						left: '63%',
						height: '60px',
						width: '80px'
					});
					var color = flag ? '#794fc4' : '#ffffff';
					let line = ''
					if (that.navIndex == 2) {
						line = '<font color="' + color +
							'" style="font-size:24px;">\u2014</font>'
					}
					view.drawRichText('<font color="' + color +
						'" style="font-size:16px;">核销</font><br/>' + line, {
							top: '0px',
							left: '0%',
							width: '100%',
							height: 'wrap_content',
							color: 'FFFFFF'
						}, {
							size: '24px',
							family: 'Times New Roman',
							fontSrc: '/static/font/iconfont.ttf',
							align: 'center'
						});
					view.show();

					view.addEventListener("click", onClick, false);
				}

				_Create();

				function onClick() {
					that.navIndex = 2

					// plus.nativeObj.View.getViewById('flashLamp').close();
					// plus.nativeObj.View.getViewById('navItem').close();
					plus.nativeObj.View.getViewById('navItem2').close();
					plus.nativeObj.View.getViewById('navItem3').close();
					// plus.barcode.getBarcodeById('barcode').close();

					// that.navItem();
					that.navItem2();
					that.navItem3();
				}
			},
			drawTips() {
				const view = new plus.nativeObj.View('tips', {
					bottom: '26%',
					left: '0px',
					height: '60px',
					width: 'auto'
				});
				view.drawRichText('<font color="#fff" style="font-size:15px;">将二维码/条形码放到框内，即可自动扫描</font>', {
					top: '0px',
					left: '0%',
					width: '100%',
					height: 'wrap_content',
					color: 'FFFFFF'
				}, {
					size: '24px',
					family: 'Times New Roman',
					fontSrc: '/static/font/iconfont.ttf',
					align: 'center'
				});
				view.show();
			},
			closeView() {
				var back = plus.nativeObj.View.getViewById('back');
				var flashLamp = plus.nativeObj.View.getViewById('flashLamp');
				// var navItem = plus.nativeObj.View.getViewById('navItem');
				var navItem2 = plus.nativeObj.View.getViewById('navItem2');
				var navItem3 = plus.nativeObj.View.getViewById('navItem3');
				var tips = plus.nativeObj.View.getViewById('tips');

				if (back) back.close();
				if (flashLamp) flashLamp.close();
				// if (navItem) navItem.close();
				if (navItem2) navItem2.close();
				if (navItem3) navItem3.close();
				if (tips) tips.close();

			},
			closeScan() {
				var b = plus.barcode.getBarcodeById('barcode');
				if (b) {
					b.close();
				}
			},
			// 切换扫描
			switchScan() {
				this.scan();
				this.flashLamp();
				// this.navItem();
				this.navItem2();
				this.navItem3();
				this.cScanInputH = '';
			},
		},

	}
</script>

<style>

</style>
